//
//  AppDelegate.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/2/24.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "AppDelegate.h"


#import "ViewController.h"
#import "MeViewController.h"//置业顾问
#import "DejalActivityView.h"
#import "JingViewController.h"//销售经理
#import "CEOViewController.h"//CEO

#import "GuideViewController.h"

#import <UserNotifications/UserNotifications.h>

// 引入JPush功能所需头文件
#import "JPUSHService.h"
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
// 如果需要使用idfa功能所需要引入的头文件（可选）
#import <AdSupport/AdSupport.h>


@interface AppDelegate ()<UNUserNotificationCenterDelegate,JPUSHRegisterDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    BOOL isShowGuide = [ud boolForKey:@"guide"];
    if (isShowGuide == YES)
    {
        // 说明 之前肯定是保存过这个值的，并且用户已经看过引导页了
        ViewController *mainVC = [[ViewController alloc] init];
        self.window.rootViewController = mainVC;
        NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
        NSString *uid = [NSString stringWithFormat:@"%@",[userDefa objectForKey:@"UID"]];
        //判断是否已登陆
        if ([uid isEqualToString:@"1"])
        {
            //已登陆
            _meVC = [[MeViewController alloc]init];
            self.window.rootViewController = _meVC;
        }
        else if ([uid isEqualToString:@"2"])
        {
            JingViewController *jingVC =[[JingViewController alloc] init];
            self.window.rootViewController = jingVC;
            
        }
        else if ([uid isEqualToString:@"3"])
        {
            CEOViewController *ceoVC =[[CEOViewController alloc] init];
            self.window.rootViewController = ceoVC;
        }
        else
        {
            //未登录
            _firstVC = [[ViewController alloc] init];
            _firstVC.delegate = self;
            self.window.rootViewController = _firstVC;
            
        }

    }
    else
    {
        // 说明 用户没用点击过引导页上的按钮
        // 把引导页 作为 window的根视图控制器
        GuideViewController *guideVC = [[GuideViewController alloc] init];
        self.window.rootViewController = guideVC;
    }
    

    
     [NSThread sleepForTimeInterval:1.0];//设置启动页面时间
    

    // 设置一次  整个程序中所有的导航条的背景颜色等 都会改变
    //  一般写在入口类中 或者 写在所有控制器的一个父类中
  
    // 导航条的背景颜色
    UINavigationBar *navBar = [UINavigationBar appearance];
    [navBar setBarTintColor:[UIColor colorWithHexString:@"#ff8300"]];
    // 导航上除了title以外所有控件的字体颜色
    navBar.tintColor = [UIColor blackColor];
    // 导航上title字体颜色,字体的大小
    [navBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:20]}];
    
    //设置状态栏变成白色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

   
    //notice: 3.0.0及以后版本注册可以这样写，也可以继续用之前的注册方式
//    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
//    entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
//    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//        // 可以添加自定义categories
//        // NSSet<UNNotificationCategory *> *categories for iOS10 or later
//        // NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9
//    }
//    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
//
//    // Optional
//    // 获取IDFA
//    // 如需使用IDFA功能请添加此代码并在初始化方法的advertisingIdentifier参数中填写对应值
////    NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
//    
//    // Required
//    // init Push
//    // notice: 2.1.5版本的SDK新增的注册方法，改成可上报IDFA，如果没有使用IDFA直接传nil
//    // 如需继续使用pushConfig.plist文件声明appKey等配置内容，请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。
//    [JPUSHService setupWithOption:launchOptions appKey:@"84eba39938741f82dd3a903a"                          channel:@"App Store"
//                 apsForProduction:0
//            advertisingIdentifier:nil];
    
    
    
    
    
    
    return YES;
}

////注册APNs成功并上报DeviceToken,实现该回调方法并添加回调方法中的代码
//- (void)application:(UIApplication *)application
//didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
//    
//    /// Required - 注册 DeviceToken
//    [JPUSHService registerDeviceToken:deviceToken];
//}
//
////实现注册APNs失败接口（可选）
//
//
//- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//    //Optional
//    NSLog(@"APNs注册失败: %@", error);
//}
//
////添加处理APNs通知回调方法  实现该回调方法并添加回调方法中的代码
//#pragma mark- JPUSHRegisterDelegate
//
//// iOS 10 Support
//- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
//    // Required
//    NSDictionary * userInfo = notification.request.content.userInfo;
//    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//        [JPUSHService handleRemoteNotification:userInfo];
//    }
//    completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法，选择是否提醒用户，有Badge、Sound、Alert三种类型可以选择设置
//}
//
//// iOS 10 Support
//- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
//    // Required
//    NSDictionary * userInfo = response.notification.request.content.userInfo;
//    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//        [JPUSHService handleRemoteNotification:userInfo];
//    }
//    completionHandler();  // 系统要求执行这个方法
//}
//
//- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
//    
//    // Required, iOS 7 Support
//    [JPUSHService handleRemoteNotification:userInfo];
//    completionHandler(UIBackgroundFetchResultNewData);
//}
//
//- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//    
//    // Required,For systems with less than or equal to iOS6
//    [JPUSHService handleRemoteNotification:userInfo];
//}
//






- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


- (void)startLoadingWithLabelStr:(NSString *)_str
{
    if (_str != nil || ![_str isEqualToString:@""])
    {
        [DejalBezelActivityView activityViewForView:self.window withLabel:_str];
    }
    else
    {
        [DejalBezelActivityView activityViewForView:self.window];
    }
    
    NSLog(@"开始加载......");
}

- (void)stopLoading
{
    [DejalBezelActivityView removeViewAnimated:YES];
    NSLog(@"加载结束......");
}



@end
